var People = Backbone.Model.extend({
   subalbum: function() { return 'c' + gallery._currentPid; }
});

var PeopleCollection = Backbone.Collection.extend({
    model: People,
    comparator: function(item) {
        return item.get('pid');
    }
});

var IndexView = Backbone.View.extend({
    el: $('#star_people'),
    indexTemplate: $("#star_people_tmpl").template(),

    render: function() {
        var sg = this;
        $(this.el).fadeOut('fast', function() {
        $(sg.el).empty();
        $.tmpl(sg.indexTemplate, sg.model.shuffle().slice(0,4)).appendTo(sg.el);
        $(sg.el).fadeIn('fast');
        });
        return this;
    },
    
    show: function(bShow) {
        if (bShow) {
            $('#people_index_div').show();
        } else {
            $('#people_index_div').hide();
        }
    }
});

var PeoplePage = Backbone.Router.extend({
    _index: null,
    _data:null,
    _peoples:null,

    routes: {
        "": "index",
    },

    initialize: function(options) {
    
        var ws = this;
       
        if (this._index === null){
            $.ajax({
                url: 'data/people.json',
                dataType: 'json',
                data: {},
                success: function(data) {
                    ws._data = data;
                    ws._peoples = new PeopleCollection(data);
                    ws._index = new IndexView({model: ws._peoples});
                    Backbone.history.loadUrl();
                }
            });
            return this;
        }
        return this;
    },


    /**
     * Handle rendering the initial view for the application
     * @type function
    */
    index: function(id) {
        if (this._index === null)
            {
                console.log('???');
                return;
            }
        this._index.show(true);
        this._index.render();
    }
});


people_page = new PeoplePage();
Backbone.history.start();